<?php

defined('BASEPATH') or die('forbidden');

class Strategy_Controller Extends Controller {

	
    function _init() {
        check_level(50, segment(0), segment(1), segment(2, 'index'));
        require_once(CONFIGPATH . 'inc.php');
    }

    function index() {

        $this->view->display("activity/strategy/index");
    }

    function flexigrid() {
        list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();
        
        //print_r($colsinfo);
        
        $rows = array();       

        $sql = "SELECT COUNT(id) AS num FROM act_strategy WHERE {$where}";
        $one = $this->db->one($sql);
        $total = $one['num'];
        $selectSql="SELECT {$colsinfo} FROM act_strategy WHERE {$where}  ORDER BY {$sortname} {$sortorder}  LIMIT {$offset},{$rp}"; 
        $this->db->query($selectSql);      
        while ($n = $this->db->get_array('default', MYSQL_NUM)) {
        	//$n[2]=$this->inc['game_list'][$n[2]];
            $rows[] = array('id' => $n[0], 'cell' => array_values($n));
        }
        $result = array(
            'page' => $page,
            'total' => $total,
            'rows' => $rows
        );
        echo json_encode($result);
    }

    function add($id = 0) {
        if (is_submit()) {
            $rowdata = $_POST['row'];
            $selectSql="select id from act_strategy where strategyid = '$rowdata[strategyid]'";   
            //echo $selectSql;   
			$cRow=$this->db->one($selectSql);
			if ($cRow['id']>0)
			{
				$message = " add new strategy error strategyid:".$rowdata['strategyid']."  already exists !";
				//echo $message;
				echo "<script>alert('".$message."');</script>";
				cp_tips('error', $rowdata['strategyid']);
			}
			else 
			{
				$this->db->insert('act_strategy', $rowdata, true);
            	insert_log(translate('MENU_ACTIVITY_STRATEGY'),translate('ADD'), array('strategyid'=>$rowdata['strategyid']));
            	cp_tips('success', $rowdata['strategyid']);
			}
        }
        $this->view->display("activity/strategy/add");
    }

    function edit($id) {
        if (!$id)
            error(translate('C_INVALID_PARAM_ID', $id));
        $row = $this->db->one("SELECT id FROM act_strategy WHERE id={$id}");
        if (!is_array($row))
            error(translate('C_INVALID_PARAM_ID', $id));
        if (is_submit()) {
            $rowdata = $_POST['row'];
            $result = $this->db->update('act_strategy', $rowdata, array('id' => $id));
            if ($result) {
                $rowdata['id'] = $row['id'];
                insert_log(translate('MENU_ACTIVITY_STRATEGY'),translate('EDIT'),$row, $rowdata);
                cp_tips('success', $id);
            } else {
                error(translate('C_TIPS_ERROR_TEXT'));
            }
        }
        $row=$this->db->one("select * from act_strategy where id=$id ");
        $this->view->display("activity/strategy/edit", array('row' => $row));
    }

}